//下载模板 选择模板使用
//用过配置文件 获取模板信息(有哪些模板)
import { repoList, tagList, downloadLocal } from "./utils/git";
import { downLoadDirectory } from "./utils/constants";
import ora from "ora"; //进度条
import inquirer from "inquirer"; //命令交互
let { promisify } = require("util");
let MetalSmith = require("metalsmith"); //遍历文件夹，找需不需要渲染
let path = require("path");
let fs = require("fs");
//consolidate 统一所有的模板引擎
let { render } = require("consolidate").ejs;
render = promisify(render);
let ncp = require("ncp");
ncp = promisify(ncp);

let install = async () => {
	let loading = ora("fetching template ......");
	loading.start();
	let list = await repoList();
	loading.succeed();
	list = list.map(({ name }) => name);
	//console.log(list);
	let answer = await inquirer.prompt([
		{
			type: "list",
			name: "project",
			choices: list,
			questions: "pleace choice template",
		},
	]);
	// console.log(answer.project);
	//拿到当前项目
	let project = answer.project;
	//获取当前的项目的版本号
	loading = ora("fetching tag ......");
	loading.start();
	list = await tagList(project);
	loading.succeed();
	list = list.map(({ name }) => name);
	let answer2 = await inquirer.prompt([
		{
			type: "list",
			name: "tag",
			choices: list,
			questions: "pleace choice tag",
		},
	]);
	let tag = answer2.tag;
	//下载文件(先下载到缓存区文件中)
	//zf-cli init
	//下载中...
	loading = ora("download project ......");
	loading.start();
	//console.log(project,tag);
	await downloadLocal(project, tag);
	loading.succeed();
	let answer3 = await inquirer.prompt([
		{
			type: "input",
			name: "projectName",
			questions: "pleace input ProjectName",
		},
	]);
	let projectName = answer3.projectName;
	console.log(projectName);
	if (
		!fs.existsSync(path.join(downLoadDirectory + "/" + project, "ask.js"))
	) {
		//复杂的需要模板熏染 渲染后再拷贝
		//把template下的文件 拷贝到执行命令的目录下
		//在这个目录下 项目名字是否已经存在 如果存在示当前已经存在
		await ncp(downLoadDirectory + "/" + project, path.resolve(projectName));
	} else {
		//复杂的模板  把git上的项目下载下来，如果有ask文件就是一个复杂的模板，我们需要用户选择，选择后编译模板
		// metalsmith--模板编译需要这个包
		//需要渲染模板的接口：https://github.com/zhu-cli/vue-template/blob/master/package.json
		//1.让用户填信息
		await new Promise((resolve, reject) => {
			MetalSmith(__dirname) //如果你传入路径，默认遍历当前路径下的src文件夹
				.source(downLoadDirectory + "/" + project)
				.destination(path.resolve(projectName))
				.use(async (files, metal, done) => {
					// console.log(files)
					let args = require(path.join(
						downLoadDirectory + "/" + project,
						"ask.js"
					));
					let obj = await inquirer.prompt(args);
					// console.log(obj);//用户填写的结果
					let meta = metal.metadata();
					Object.assign(meta, obj);
					delete files["ask.js"];
					done();
				})
				.use((files, metal, done) => {
					let obj = metal.metadata();
					Reflect.ownKeys(files).forEach(async (file) => {
						//是要处理的文件
						if (file.includes("js") || file.includes("json")) {
							let content = files[file].contents.toString(); //文件的内容
							if (content.includes("<%")) {
								conteny = await render(content, obj);
								files[file].contents = Buffer.from(content); //渲染结果
							}
						}
					});
					//2.让用户填写的信息取渲染模板
					//根据用户新的输入 下载模板
					// console.log(metal.metadata())
					done();
				})
				.build((err) => {
					if (err) {
						reject();
					} else {
						resolve();
					}
				});
		});
	}
};

export default install;
